package com.siyao.redvip.ui.activity;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;
import com.android.volley.http.RequestManager;
import com.android.volley.http.RequestManager.RequestListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.siyao.redvip.R;
import com.siyao.redvip.bean.UpdateVersion;
import com.siyao.redvip.components.view.LoadingDialog;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.service.NewVersionUpdateService;
import com.siyao.redvip.utils.AppUtils;
import com.siyao.redvip.utils.Constants;
import com.siyao.redvip.utils.ToastUtils;

/**
 * 首页欢迎页
 * Created by lenovo on 2015/4/21.
 */
public class WelcomeActivity extends Activity {
	
	private static final String TAG = "WelcomeActivity";
	private static final String APP_PACK_INFO="com.siyao.redvip";
	private static final long DELAY_TIME = 700L;
	
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcome);
        //检查更新，版本号
        checkAppVersion();
        redirectByTime();//跳转到登陆页去
    }
   
    
    private void redirectByTime() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
		        finish();
		        overridePendingTransition(R.anim.zoom_enter,R.anim.zoom_exit);
			}
		}, DELAY_TIME);
	}
    
    private  void  checkAppVersion(){
    	Map<String,String> parmes = new HashMap<String, String>();
    	parmes.put("version", AppUtils.getVersionCode(this)+"");
    	RequestManager.getInstance(this).post(NetConstans.VERSION_URL, parmes, new RequestListener() {			
			@Override
			public void onSuccess(String response, String url, int actionId) {
				Gson gson = new Gson();
				UpdateVersion  upBean= gson.fromJson(response, new TypeToken<UpdateVersion>() {}.getType());
				if(upBean.getStatus()==1){
					if(upBean.getUpdate()==1){
						String updateUrl=upBean.getData();
						AppUtils.downloadApk(WelcomeActivity.this, updateUrl);
						ToastUtils.makeMesageShort(WelcomeActivity.this, "正在下载最新的版本，下载成功之后请重新登录!");
						
					}
				}else{
					ToastUtils.makeMesageShort(WelcomeActivity.this, getString(R.string.server_error));
				}
			}
			
			@Override
			public void onRequest() {
				
			}
			
			@Override
			public void onError(String errorMsg, String url, int actionId) {
			}
		}, 2);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	JPushInterface.onResume(this);
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	JPushInterface.onPause(this);
    }
    
    /*
     * 
     *  弹出对话框的步骤：
     *  1.创建alertDialog的builder.  
     *  2.要给builder设置属性, 对话框的内容,样式,按钮
     *  3.通过builder 创建一个对话框
     *  4.对话框show()出来  
     */ 
    protected void showUpdataDialog() { 
        AlertDialog.Builder builer = new Builder(this) ;  
        builer.setTitle("版本升级"); 
        builer.setMessage("有新版本是否更新?"); 
        //当点确定按钮时从服务器上下载 新的apk 然后安装   
        builer.setPositiveButton("确定", new OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
//                Log.i(TAG,"下载apk,更新"); 
        		dialog.dismiss();
        	//下载apk,并且安装
        		
            }    
        }); 
        //当点取消按钮时进行登录  
        builer.setNegativeButton("取消", new OnClickListener() { 
            public void onClick(DialogInterface dialog, int which) {
            	dialog.dismiss();
            	redirectByTime();//跳转到登陆页去
            } 
        }); 
        AlertDialog dialog = builer.create(); 
        dialog.show(); 
    }
    
    /*
     * 从服务器中下载APK
     */ 
    protected void downLoadApk(String updateUrl) {
    	
        final ProgressDialog pd;    //进度条对话框
        pd = new  ProgressDialog(this); 
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
        pd.setMessage("正在下载更新"); 
        pd.show(); 
        String appName="redvip";
    }
    
  //安装apk   
    protected void installApk(File file) { 
        Intent intent = new Intent(); 
        //执行动作  
        intent.setAction(Intent.ACTION_VIEW); 
        //执行的数据类型  
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");//此处Android应为android，否则造成安装不了   
        startActivity(intent); 
    }
}
